package tools.twitter;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class URLTools {
	
	private static Pattern urlPattern = Pattern.compile("https?://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]", Pattern.CASE_INSENSITIVE);
	
	/**
	 * Given a tweet, return the URLs it contains
	 * @param tweet
	 * @return
	 */
	public static ArrayList<String> getURLs(String tweet) {
		ArrayList<String> urls = new ArrayList<>();
		Matcher m = urlPattern.matcher(tweet);
		while (m.find()) {
			urls.add(m.group());
		}
		
		return urls;
	}
	
	/**
	 * Given a maybe malformed url, return the URL
	 * @param url
	 * @return
	 */
	public static String getURL(String url, String prefix) {
		String realURL;
		Matcher m = urlPattern.matcher(url);
		if (m.find()) {
			realURL = m.group();
		} else {
			realURL = prefix + url;
		}
		
		return realURL;
	}
	
	
}
